﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

namespace INFTUR
{
    public partial class ManterLocadora : System.Web.UI.Page
    {
        public static bool insere = false;
        public void Limpar()
        {
            tbId.Text = "";
            tbNome.Text = "";          
            tbSite.Text = "";          
            tbTelefone.Text = "";           
            tbEmail.Text = "";         
            tbEndereco.Text = "";
            lbErro.Text = "";
           

            tbNome.Enabled = false;
            tbSite.Enabled = false;
            tbTelefone.Enabled = false;
            tbEmail.Enabled = false;
            tbEndereco.Enabled = false;

            btGravar.Enabled = false;
            btCancelar.Enabled = false;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btGravar_Click(object sender, EventArgs e)
        {
            try
            {
                LocadoraBO lBO = new LocadoraBO();

                lBO.Nome = tbNome.Text;
                lBO.Site = tbSite.Text;
                lBO.Telefone = tbTelefone.Text;
                lBO.Email = tbEmail.Text;
                lBO.Endereco = tbEndereco.Text;

                LocadoraBOMetodos LBOMetodos = new LocadoraBOMetodos();
                if (insere)
                {
                    LBOMetodos.inserir(lBO);
                }
                else
                {
                    lBO.Id = Convert.ToInt32(tbId.Text);
                    LBOMetodos.atualizar(lBO);
                }
                    Limpar();
                    GridView1.DataBind();
            }

            catch (Exception ex)
            {
                lbErro.Text = ex.Message;
            }
           
        }

        protected void btCancelar_Click(object sender, EventArgs e)
        {
            Limpar();
        }

        protected void btNovo_Click1(object sender, EventArgs e)
        {
            insere = true;
            tbNome.Enabled = true;
            tbSite.Enabled = true;
            tbTelefone.Enabled = true;
            tbEmail.Enabled = true;
            tbEndereco.Enabled = true;

            btGravar.Enabled = true;
            btCancelar.Enabled = true;
        }

        protected void lbAtualizar_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;
            int id = Convert.ToInt32(linha.Cells[1].Text);

            LocadoraBOMetodos lBOM = new LocadoraBOMetodos();
            LocadoraBO lBO = new LocadoraBO();
            lBO = lBOM.consultar(id);

            tbId.Text = Convert.ToString(lBO.Id);
            tbNome.Text = lBO.Nome;
            tbSite.Text = lBO.Site;
            tbTelefone.Text = lBO.Telefone;
            tbEmail.Text = lBO.Email;
            tbEndereco.Text = lBO.Endereco;

            tbNome.Enabled = true;
            tbSite.Enabled = true;
            tbTelefone.Enabled = true;
            tbEmail.Enabled = true;
            tbEndereco.Enabled = true;

            btGravar.Enabled = true;
            btCancelar.Enabled = true;
            GridView1.DataBind();

        }

        protected void lbDeletar_Click(object sender, EventArgs e)
        {
            bool erro = true;
            LinkButton lb = (LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;
            int id = Convert.ToInt32(linha.Cells[1].Text);

            LocadoraBOMetodos lBOM = new LocadoraBOMetodos();
            LocadoraBO lBO = new LocadoraBO();
            erro = lBOM.remover(id);
            GridView1.DataBind();
        }


    }
}
